﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;

namespace MidSoft.Sys
{
    public partial class AddPermission : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindMenu();
        }
        public void BindMenu()
        {
            ChinaSoft.BLL.BLL_tb_MidSoft_PowerList bllPowerList = new ChinaSoft.BLL.BLL_tb_MidSoft_PowerList();
            DataTable dt = bllPowerList.GetAllList().Tables[0];
            this.ddlFather.Items.Clear();
            this.ddlFather.Items.Add(new ListItem("无上级", "0"));
            DataRow[] drs = dt.Select("FatherID= 0");

            foreach (DataRow r in drs)
            {
                string nodeid = r["PowerId"].ToString();
                string text = r["PowerName"].ToString();
                string parentid = r["FatherID"].ToString();
                text = "┣" + text;//╋
                this.ddlFather.Items.Add(new ListItem(text, nodeid));
                int sonparentid = int.Parse(nodeid);
                string blank = "├";//┠

                BindNode(sonparentid, dt, blank);

            }
            this.ddlFather.DataBind();
        }

        private void BindNode(int parentid, DataTable dt, string blank)
        {
            DataRow[] drs = dt.Select("FatherID= " + parentid);

            foreach (DataRow r in drs)
            {
                string nodeid = r["PowerId"].ToString();
                string text = r["PowerName"].ToString();
                text = blank + "『" + text + "』";

                this.ddlFather.Items.Add(new ListItem(text, nodeid));
                int sonparentid = int.Parse(nodeid);
                string blank2 = blank + "─";

                BindNode(sonparentid, dt, blank2);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strErr = "";
            if (this.txtPowerName.Text.Trim().Length == 0)
            {
                strErr += "权限名不能为空！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string PowerName = this.txtPowerName.Text;
            int FatherID = int.Parse(ddlFather.SelectedItem.Value);
            DateTime CreateTime = DateTime.Now;
            int CreateUserId = 0;
            if (Session["currentUserID"] != null && Session["currentUserID"].ToString() != "")
                CreateUserId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);
            int Sort =ChinaSoft.Common.Input.SetInt(this.txtSort.Text);
            #region 添加
            ChinaSoft.Model.Model_tb_MidSoft_PowerList model = new ChinaSoft.Model.Model_tb_MidSoft_PowerList();
            model.PowerName = PowerName;
            model.FatherID = FatherID;
            model.CreateTime = CreateTime;
            model.CreateUserId = CreateUserId;
            model.Sort = Sort;
            model.MenuType = ChinaSoft.Common.Input.SetInt(ddlMenuType.SelectedValue);
            ChinaSoft.BLL.BLL_tb_MidSoft_PowerList bll = new ChinaSoft.BLL.BLL_tb_MidSoft_PowerList();
            int permissionId=bll.Add(model);
            #endregion
            #region 添加日志
            ChinaSoft.Model.Model_tb_MidSoft_log modelLog = new ChinaSoft.Model.Model_tb_MidSoft_log();
            ChinaSoft.BLL.BLL_tb_MidSoft_log bllLog = new ChinaSoft.BLL.BLL_tb_MidSoft_log();
            modelLog.userAccount = "";
            if (Session["UserAccount"] != null && Session["UserAccount"].ToString() != "")
                modelLog.userAccount = Session["UserAccount"].ToString();
            modelLog.userName = "";
            if (Session["UserName"] != null && Session["UserName"].ToString() != "")
                modelLog.userName = Session["UserName"].ToString();
            modelLog.description = "权限信息添加,ID=" + permissionId + ",PowerName="+PowerName;
            modelLog.LogType = 1;
            modelLog.CreateTime = DateTime.Now;
            bllLog.Add(modelLog);
            #endregion
            Response.Write("<script>alert('成功！');top.window.parent.frames.item('workspace').location.reload();window.parent.$('#DivSysiframe').dialog(\"close\");</script>");
        }
    }
}